package com.loong.nnm.utils;

import net.juniper.netconf.NetconfSession;
import net.juniper.netconf.XML;
import net.juniper.netconf.XMLBuilder;
import net.juniper.netconf.element.Datastore;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.xml.parsers.ParserConfigurationException;
import java.util.Objects;
@Component
public class XmlBuilderUtils {
    private static final String GET = "get";
    private static final String FILTER = "filter";
    private static final String Attribute_name_type = "type";
    private static final String Attribute_value_type = "subtree";
    private static final String EDIT_CONFIG = "edit-config";
    private static final String CONFIG = "config";


    public static XMLBuilder instanceXMLBuilder() {
        try {
            return new XMLBuilder();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 包装get操作的外套层
     *
     * @return get的外套层
     */
    public static XML getXMlCoat(){
        XML get = Objects.requireNonNull(instanceXMLBuilder()).createNewRPC(GET, FILTER);
        get.setAttribute(Attribute_name_type, Attribute_value_type);
        return get;
    }

    /**
     * 包装edit-config操作的外套层
     *
     * @return edit-config的外套层
     */
    public static XML editConfigXMLCoat(){
        XML target = Objects.requireNonNull(instanceXMLBuilder()).createNewXML(EDIT_CONFIG, "target");
        target.append(Datastore.RUNNING.toString());
        target.addSibling(CONFIG);
        return target;
    }


}
